﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.ZhiWu;
using Enterprise.Service.ZhiWu;
using Enterprise.Control;
using Enterprise.Model.User;
using Enterprise.Service.User;
using Enterprise.Model.Department;
using Enterprise.Service.Department;
namespace Enterprise.UI.Web.Manage.ZhiWu
{
    public partial class UserZhiwuManage : System.Web.UI.Page
    {
        int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnBindData();
            OnBindDepartMent();
        }

        private void OnBindDepartMent()
        {
            DepartmentService dService = new DepartmentService();
            var list = dService.DepartmentList().OrderBy(p => p.DORDERBY);
            tb_Dept.DataSource = list;
            tb_Dept.DataTextField = "DNAME";
            tb_Dept.DataValueField = "DEPTID";
            tb_Dept.DataBind();
            tb_Dept.Items.Insert(0, new ListItem("请选择部门", "0"));
        }

        private void OnBindData()
        {
            ZhiwuService zwService = new ZhiwuService();
            var q = zwService.ZhiWuDisp(Id);
            if (q != null)
            {
                lb_Name.Text = lb_Name1.Text = q.ZWNAME;
                lb_Remark.Text = q.ZWREMARK;
            }

            if (Id == 0)
                return;

            UserZhiwuService uzService = new UserZhiwuService();
            List<UserZhiwuEntity> list = uzService.UserZhiWuList().Where(p => p.ZWID == Id).ToList();
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onMouseOver"] = "js.ItemOver(this)";
            }
        }

        protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument); //获取行号
            int did = (int)GridView1.DataKeys[index].Value;
            UserZhiwuService ser = new UserZhiwuService();
            var q = ser.UserZhiWuDisp(did);
            if (q != null)
            {
                switch (e.CommandName)
                {
                    case "mDelete":
                        q.DB_Option_Action = "Delete";
                        ser.UserZhiWuExecute(q);
                        break;
                }
                OnStart();
            }
        }

        protected void Button1_OnClick(object sender, EventArgs e)
        {
            UserZhiwuService uzService = new UserZhiwuService();
            UserZhiwuEntity entity = new UserZhiwuEntity();
            entity.ZWID = Id;
            int userId = (int)Utility.sink(tb_User.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            if (userId != 0)
            {
                entity.USERID = userId;
                if (uzService.UserZhiWuCheck(entity.ZWID, userId)==false)
                {
                    entity.DB_Option_Action = "Insert";
                    uzService.UserZhiWuExecute(entity);
                    Response.Redirect("UserZhiwuManage.aspx?Id=" + Id);
                    Response.End();
                }
            }
        }
        protected void tb_Dept_SelectedIndexChanged(object sender, EventArgs e)
        {
            tb_User.Items.Clear();
            int deptId = (int)Utility.sink(tb_Dept.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            if (deptId != 0)
            {
                UserService uService = new UserService();
                var list = uService.UserList().Where(p => p.DEPTID == deptId).OrderBy(p => p.UORDERBY);
                tb_User.DataSource = list;
                tb_User.DataTextField = "UNAME";
                tb_User.DataValueField = "USERID";
                tb_User.DataBind();
                tb_User.Items.Insert(0, new ListItem("请选择人员", "0"));
            }
        }

    }
}